﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using Telerik.Web.UI;
using _WebShared.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{ 
	public partial class DatePicker : System.Web.UI.UserControl
	{
		private void Page_Load(object sender, EventArgs e)
		{

		}

		public string StyleTextBox
		{
			set { SetTextBoxStyle(value); }
		}

		public void SetTextBoxStyle(string cssStrings)
		{
			try
			{
				List<string> arrayCss = cssStrings.Split(';').ToList();

				var arrayCssKv = new List<KeyValuePair<string,string>>(); 
				foreach(string eachCss in arrayCss)
				{
					if (eachCss.Split(':').Count()==2)
						arrayCssKv.Add(new KeyValuePair<string, string>(eachCss.Split(':')[0], eachCss.Split(':')[1]));
				}
				/*var arrayCssKv =
					arrayCss.Select(css => new KeyValuePair<string, string>(css.Split(':')[0], css.Split(':')[1])).ToList();*/
				var cssKey = new List<string>();
				foreach(var key in txtDate.Style.Keys)
				{
					cssKey.Add((string)key);
				}
				foreach (KeyValuePair<string, string> cssString in arrayCssKv)
				{
					if (!cssKey.Contains(cssString.Key))
						txtDate.Style.Add(cssString.Key, cssString.Value);
					else 
						txtDate.Style[cssString.Key] = cssString.Value;
				}
			} catch (Exception err)
			{
				var errMsg = err.Message;
			}
		}

		public void SetValue(object date)
		{
			if (date != null)
			{
				switch (date.GetType().ToString())
				{
					case "System.DateTime":
					case "System.DateTime?":
						try
						{
							txtDate.Value = ((DateTime)date).ToShortDateString();
						}
						catch (Exception err)
						{
							txtDate.Value = "Error-" + err.Message;
						}
						break;

					default: // System.String
						try
						{
							string dateStr = date.ToString();
							if (dateStr.EndsWith("00:00:00")) dateStr = dateStr.Substring(0, dateStr.Length - 8);
							else if (dateStr.EndsWith("0:00:00")) dateStr = dateStr.Substring(0, dateStr.Length - 7);
							else if (dateStr.EndsWith("00:00")) dateStr = dateStr.Substring(0, dateStr.Length - 5);
							else if (dateStr.EndsWith("0:00")) dateStr = dateStr.Substring(0, dateStr.Length - 4);
							else if (!String.IsNullOrEmpty(dateStr))
							{
								// "24/12/2555 15:14:21";
								if( dateStr.IndexOf(' ') != -1)
									dateStr = dateStr.Substring(0, dateStr.IndexOf(' '));
							}
							txtDate.Value = dateStr.Trim();
						}
						catch (Exception err)
						{
							txtDate.Value = "Error-" + err.Message;
						}
						break;
				}
			}
			else
			{
				txtDate.Value = "";
			}
		}

		public string GetStringValue()
		{
			string strDate = txtDate.Value;

			return strDate;
		}

		public String Value
		{
			get
			{
				var dt = GetDateNullableValue();
				return (dt ?? DateTime.MinValue).ToShortDateString();
			}
			set
			{
				SetValue(value);
			}
		}

		public DateTime GetDateValue()
		{
			string strDate = txtDate.Value;

			if (String.IsNullOrEmpty(strDate))
				return DateTime.MinValue;

			try
			{
				return Convert.ToDateTime(strDate, new CultureInfo("th-TH"));
			}
			catch (Exception e)
			{
				DateTime answer;
				DateTime.TryParseExact(strDate, "d/M/yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out answer);
				return answer;
			} 
		}

		public DateTime? GetDateNullableValue()
		{
			string strDate = txtDate.Value;
			DateTime? date = String.IsNullOrEmpty(strDate) ? (DateTime?)null : Convert.ToDateTime(strDate);

			return date;
		}

		public override string ToString()
		{
			return GetStringValue();
		}

	}
}